package com.nondo.rdp.finance.endpoint.api.app.Activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nondo.rdp.auth.common.UserUtils;
import com.nondo.rdp.auth.domain.User;
import com.nondo.rdp.auth.endpoint.model.ConvertCall;
import com.nondo.rdp.auth.endpoint.model.Page;
import com.nondo.rdp.auth.endpoint.model.Query;
import com.nondo.rdp.core.model.View;
import com.nondo.rdp.finance.domain.activity.MarketCourse;
import com.nondo.rdp.finance.domain.activity.MarketRecord;
import com.nondo.rdp.finance.domain.course.Course;
import com.nondo.rdp.finance.domain.course.CourseBuy;
import com.nondo.rdp.finance.model.course.CourseQuery;
import com.nondo.rdp.finance.service.activity.MarketCourseService;
import com.nondo.rdp.finance.service.activity.MarketRecordService;
import com.nondo.rdp.finance.service.course.CourseBuyService;
import com.nondo.rdp.finance.service.course.CourseService;

@RestController("apiAppMarketCourse")
@RequestMapping("/api/app/marketCourse")
public class MarketCourseEndpoint {
	@Autowired
	CourseService courseService;

	@Autowired
	CourseBuyService courseBuyService;

	@Autowired
	MarketCourseService marketCourseService;

	@Autowired
	MarketRecordService marketRecordService;

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping("/info")
	public View info(HttpServletRequest request, HttpServletResponse response) {
		View view = new View(20000, "尚未领取");
		User user = UserUtils.getUser(request, response);
		int isReceive = marketRecordService.getOne(user.getId(), 0);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("isReceive", isReceive);
		if (isReceive == 1) {
			view.setMessage("已领取");
		}
		view.setData(resultMap);
		return view;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping("/receive")
	public View receive(HttpServletRequest request, HttpServletResponse response) {
		View view = new View(20000, "领取成功");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		User user = UserUtils.getUser(request, response);
		Query<MarketCourse> query = new Query<MarketCourse>();
		Page<MarketCourse> page = marketCourseService.findAll(new Page<MarketCourse>(1, 5), query.getData());
		List<MarketCourse> marketCourseList = page.getContent();
		boolean boo = true;
		resultMap.put("isBuy", "0");
		int isReceive = marketRecordService.getOne(user.getId(), 0);
		if (isReceive == 1) {
			view.setMessage("你已参加过该活动");
			resultMap.put("isBuy", "1");
			view.setData(resultMap);
			return view;
		}
		for (MarketCourse marketCourse : marketCourseList) {
			Course course = marketCourse.getCourse();
			int isBuy = courseBuyService.getOne(course.getId(), user.getId());
			if (isBuy == 1) {
				continue;
			} else {
				boo = false;
			}
			CourseBuy courseBuy = new CourseBuy();
			courseBuy.setCourse(course);
			courseBuy.setUser(user);
			this.courseBuyService.save(courseBuy);
		}
		MarketRecord marketRecord = new MarketRecord();
		marketRecord.setUser(user);
		marketRecord.setType(0);
		this.marketRecordService.save(marketRecord);
		if (boo) {
			resultMap.put("isBuy", "1");
			view.setMessage("你已购买所有赠送课程");
		}
		view.setData(resultMap);

		return view;
	}

	/**
	 * 获取营销课程列表
	 * 
	 * @param request
	 * @param response
	 * @param query
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping("/list")
	public View<Page> list(HttpServletRequest request,
			HttpServletResponse response, @RequestBody Query<MarketCourse> query) {
		View<Page> view = new View(20000, "获取列表成功");

		MarketCourse marketCourse = query.getData();

		Page<MarketCourse> page = marketCourseService.findAll(
				new Page<MarketCourse>(query.getPage(), query.getSize()),
				marketCourse);

		Page<CourseQuery> resultPage = page
				.convertPage(new ConvertCall<MarketCourse, Page<CourseQuery>>() {

					@Override
					public Page<CourseQuery> execute(Page<MarketCourse> srcPage) {
						Page<CourseQuery> courseQueryPage = new Page<CourseQuery>(
								srcPage.getPageNumber(), srcPage.getPageSize(),
								srcPage.getTotalPages(), srcPage.getTotalElements());
						List<CourseQuery> courseQueryList = new ArrayList<CourseQuery>();
						List<MarketCourse> marketCourseList = srcPage.getContent();
						for (MarketCourse marketCourse : marketCourseList) {
							CourseQuery courseQuery = CourseQuery.covert2Bean(marketCourse.getCourse());
							courseQuery.setId(marketCourse.getId());
							courseQueryList.add(courseQuery);
						}
						courseQueryPage.setContent(courseQueryList);
						return courseQueryPage;
					}

				});

		view.setData(resultPage);

		return view;
	}

}